﻿using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Net.Mail;
using System.Collections.Generic;


/// <summary>
/// MailService 的摘要描述
/// </summary>

namespace com.pro2e.console.generic.mail
{
    public class MailService
    {
        log4net.ILog log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);

        private string smtpHost;
        private int port = 25;
        private bool enableSsl = true;
        private string credentialsID;
        private string credentialsPW;

        /// <summary>
        /// 用於多個mail分開
        /// </summary>
        private static readonly char SPLIT = ';';


        public MailService(string smtpHost)
        {
            this.smtpHost = smtpHost;
        }

        public MailService(string smtpHost, int port)
        {
            this.smtpHost = smtpHost;
            this.port = port;
        }

        public MailService(string smtpHost, int port, bool enableSsl, string credentialsID, string credentialsPW)
        {
            this.smtpHost = smtpHost;
            this.port = port;
            this.enableSsl = enableSsl;
            this.credentialsID = credentialsID;
            this.credentialsPW = credentialsPW;
           
        }

        public void SendMail(MailAddress from, MailAddress to, string subject, string body)
        {
            List<MailAddress> mailAddressList = new List<MailAddress>();
            mailAddressList.Add(to);
            SendMail(from, to, subject, body);
        }


        public void SendMail(string from, string to, string subject, string body)
        {
            SendMail(from, to, subject, body, null);
        }

        public void SendMail(string from, string to, string subject, string body, string bcc)
        {
            MailAddress mailfrom = new MailAddress(from);

            List<MailAddress> mailAddressList = PutMailAddress(to);
            List<MailAddress> mailBccAddressList = PutMailAddress(bcc);

            SendMail(mailfrom, mailAddressList, subject, body, mailBccAddressList);
        }


        public void SendMail(MailAddress from, List<MailAddress> to, string subject, string body)
        {
            SendMail(from, to, subject, body, null);
        }

        public void SendMail(MailAddress from, IList<MailAddress> to, string subject, string body, IList<MailAddress> bcc)
        {

            MailMessage em = new MailMessage();
            em.From = new MailAddress(from.Address, "科技與表演藝術旗艦計畫");
 
            
            foreach (MailAddress mail in to)
            {
                em.To.Add(mail);
            }

            if (bcc != null && bcc.Count > 0)
            {
                foreach (MailAddress mail in bcc)
                {
                    em.Bcc.Add(mail);
                }
            }

            em.SubjectEncoding = System.Text.Encoding.UTF8;
            em.BodyEncoding = System.Text.Encoding.UTF8;

            //信件主題 
            em.Subject = subject;
            //內容 
            em.Body = body;
            em.IsBodyHtml = true;
            System.Net.Mail.SmtpClient client = new System.Net.Mail.SmtpClient();

            //登入帳號認證  
            if (!string.IsNullOrEmpty(credentialsID) && !string.IsNullOrEmpty(credentialsPW))
            {
                client.Credentials = new System.Net.NetworkCredential(credentialsID, credentialsPW);
            }

            //使用587 Port 
            client.Port = port;
            client.Host = smtpHost;
            //啟動SSL 
            //client.EnableSsl = enableSsl;
            //寄出 
            client.Send(em);
        }
      

        private List<MailAddress> PutMailAddress(string mailStr)
        {
            List<MailAddress> MailList = new List<MailAddress>();

            if (!string.IsNullOrEmpty(mailStr))
            {
                mailStr = mailStr.Trim();

                if (mailStr.IndexOf(SPLIT) != -1)
                {
                    foreach (string mail in mailStr.Split(SPLIT))
                    {
                        MailList.Add(new MailAddress(mail));
                    }
                }
                else
                {
                    log.Debug("mail:" + mailStr);
                    MailList.Add(new MailAddress(mailStr));
                }

                return MailList;
            }
            else
            {
                return null;
            }
        }




        /*寄信範例
        public static void main()
        {
            MailService mailService = new MailService("smtp.gmail.com", 587, true, "shuhao.hu@gmail.com", "aaa");
            mailService.SendMail("shuhao.hu@gmail.com", "shuhao@ms63.url.com.tw", "test", "body");
        }
        */
    }
}